package com.uchengwang.ui.cm_ui.cycle.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleThemeBean;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2018/5/26.
 */

public class CycleThemesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<CycleThemeBean> data = new ArrayList<>();

    public CycleThemesAdapter(Context context){
        this.context = context;
    }

    public void setData(List<CycleThemeBean> data){
        this.data = data;
        notifyDataSetChanged();
    }

    public List<CycleThemeBean> getData(){
        return data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_cycle_themes, parent, false);
        return new CycleThemesVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        CycleThemesVH holder = (CycleThemesVH) h;
        final CycleThemeBean bean = data.get(position);
        holder.cycle_name.setText(bean.getItemName());

        if (bean.isCheck()){
            ImageManager.getInstance().loadImage(context, holder.cycle_image, R.drawable.icon_room_check, ImageManager.ImageType.NARMOL);
        }else {
            ImageManager.getInstance().loadImage(context, holder.cycle_image, R.drawable.icon_room_normal, ImageManager.ImageType.NARMOL);
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (CycleThemeBean b: data){
                    if (b.getId() ==bean.getId()){
                        b.setCheck(true);
                    }else {
                        b.setCheck(false);
                    }
                }
//                bean.setCheck(!bean.isCheck());
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class CycleThemesVH extends RecyclerView.ViewHolder {
        private View itemView;
        private TextView cycle_name;
        private ImageView cycle_image;

        public CycleThemesVH(View view) {
            super(view);
            AutoUtils.auto(view);

            itemView = view;
            cycle_name = view.findViewById(R.id.cycle_name);
            cycle_image = view.findViewById(R.id.cycle_image);
        }
    }
}
